package com.ostrich.module_main

import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.ostrich.library_base.BaseActivity
import com.ostrich.module_main.adapter.MainPagerAdapter
import com.ostrich.module_main.custom.SpecialTab
import com.ostrich.module_main.custom.SpecialTabRound
import com.ostrich.module_main.databinding.MainActivityRoundedBinding
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/26 11:01
 * @Description : RoundedActivity 圆形底部导航
 */
class RoundedActivity:BaseActivity<MainActivityRoundedBinding>() {
    private val list:MutableList<Fragment> = mutableListOf()
    private lateinit var mainPagerAdapter: MainPagerAdapter
    override fun initView() {
//        list.add(BlankFragment())
//        list.add(BlankFragment())
//        list.add(BlankFragment())
//        list.add(BlankFragment())
        binding?.let {
            mainPagerAdapter = MainPagerAdapter(this,list)
            it.viewPager.adapter = mainPagerAdapter

            val navigationController = it.bottomView.custom()
                .addItem(newItem(R.drawable.main_home,R.drawable.main_home,"Recents"))
                .addItem(newItem(R.drawable.main_home,R.drawable.main_home,"Favorites"))
                .addItem(newRoundItem(R.drawable.main_user,R.drawable.main_user,"Nearby"))
                .addItem(newItem(R.drawable.main_home,R.drawable.main_home,"Favorites"))
                .addItem(newItem(R.drawable.main_home,R.drawable.main_home,"Recents"))

                .build()
            navigationController.setHasMessage(2,true)
            navigationController.setMessageNumber(3,8)
            //自动联动：一句话
//            navigationController.setupWithViewPager(it.viewPager)
            //手动实现上下联动：因为PageNavigationView只能和viewpager自动联动
            navigationController.addTabItemSelectedListener(object : OnTabItemSelectedListener {
                override fun onSelected(index: Int, old: Int) {
                    it.viewPager.currentItem = index
                }

                override fun onRepeat(index: Int) {
                }
            })

            it.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    navigationController.setSelect(position)
                }
            })



        }
    }

    override fun initData() {

    }

    override fun initListener() {

    }

    override fun bindLayout(): Int {
       return R.layout.main_activity_rounded
    }

    /**
     * 正常tab
     */
    private fun newItem(drawable: Int, checkedDrawable: Int, text: String): BaseTabItem? {
        val mainTab = SpecialTab(this)
        mainTab.initialize(drawable, checkedDrawable, text)
        mainTab.setTextDefaultColor(-0x777778)
        mainTab.setTextCheckedColor(-0xff6978)
        return mainTab
    }

    /**
     * 圆形tab
     */
    private fun newRoundItem(drawable: Int, checkedDrawable: Int, text: String): BaseTabItem? {
        val mainTab = SpecialTabRound(this)
        mainTab.initialize(drawable, checkedDrawable, text)
        mainTab.setTextDefaultColor(-0x777778)
        mainTab.setTextCheckedColor(-0xff6978)
        return mainTab
    }
}